﻿using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ymk_camera_surveillance.Models;

namespace ymk_camera_surveillance.Database.DBModels
{
    class DBConf
    {
        //attrs
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        [Indexed]
        public string Key { get; set; }
        [Indexed]
        public string Value { get; set; }

        //conn
        private static readonly SQLiteConnection conn = DbInstance.GetConn();
        public DBConf() { }


        public static int Set(Dictionary<string, string> key_values)
        {
            var table = conn.Table<DBConf>();

            var inserts = new List<DBConf>();
            var updates = new List<DBConf>();

            foreach (var item in key_values)
            {
                var queryRes = table.Where(o => o.Key == item.Key);
                var objs = queryRes.ToList();
                if (objs.Count == 0)
                {
                    DBConf conf = new DBConf { Key = item.Key, Value = item.Value };
                    inserts.Add(conf);
                }
                else
                {
                    foreach (var obj in objs)
                    {
                        obj.Value = item.Value;
                        updates.Add(obj);
                    }
                }
            }
            var create_num = conn.InsertAll(inserts);
            var update_num = conn.UpdateAll(updates);

            return create_num + update_num;
        }

        public static string Get(string key)
        {
            var table = conn.Table<DBConf>();
            var res = table.Where(x => x.Key == key).ToList();
            if (res.Count == 0)
            {
                return null;
            }
            else if (res.Count == 1)
            {
                return res[0].Value;
            }
            else if (res.Count > 1)
            {
                var dels = new List<int>();
                for (int i = 1; i < res.Count; i++)
                {
                    dels.Add(res[i].Id);
                }
                conn.Delete<DBConf>(dels);
                return res[0].Value;
            }
            return null;
        }

        public static Dictionary<string, string> Get(List<string> keys)
        {
            var map = new Dictionary<string, string>();
            foreach (string s in keys)
            {
                map[s] = Get(s);
            }
            return map;
        }

        public static Dictionary<string, string> GetAll()
        {
            var table = conn.Table<DBConf>();
            var confs = table.Where(x => true).ToList();
            var map = new Dictionary<string, string>();
            foreach (var conf in confs)
            {
                map[conf.Key] = conf.Value;
            }
            return map;
        }

        public static int Del(List<string> Key_s)
        {
            var table = conn.Table<DBConf>();
            var res = table.Where(c => Key_s.Contains(c.Key)).Delete();
            return res;
        }
    }
}
